﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Manage.Controller;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// PrizeTaskReocrdAjax 的摘要说明
    /// </summary>
    public class PrizeTaskReocrdAjax : IHttpHandler
    {
        PrizeTaskReocrdCotroller Controller = new PrizeTaskReocrdCotroller();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "Select":
                        GetListByState(context);
                        break;
                    default:
                        break;
                }
            }
        }
        private void GetList(HttpContext context)
        {
            int UserID = Convert.ToInt32(context.Request["typeID"]);
            List<PrizeTaskReocrdShow> list = Controller.GetList(UserID);
            string json = JsonHelper.ListToJson<PrizeTaskReocrdShow>(list);
            context.Response.Write(json);
        }
        private void GetListByState(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            string userSearchType = context.Request["userSearchType"];
            int state = Convert.ToInt32(context.Request["state"]);
            string value = context.Request["value"];
            string remark = context.Request["remark"];
            int userID = 0;
            List<PrizeTaskReocrdShow> list = Controller.GetListByState(state, userSearchType, value, remark, pageIndex, pageSize, out userID, out count);
            string json = JsonHelper.ListToJson<PrizeTaskReocrdShow>(list, count + "\",\"userID\":\"" + userID);
            context.Response.Write(json);
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="context"></param>
        private void Add(HttpContext context)
        {
            int prizeID = Convert.ToInt32(context.Request["prizeID"]);
            DateTime CreateTime = Convert.ToDateTime(context.Request["CreateTime"]);
            DateTime txtEndTime = Convert.ToDateTime(context.Request["txtEndTime"]);
            string txtRemark = context.Request["txtRemark"];
            int txtPrizeCount = Convert.ToInt32(context.Request["txtPrizeCount"]);
            bool txtIsManuallyAdd = Convert.ToBoolean(context.Request["txtIsManuallyAdd"]);
            int UserID = Convert.ToInt32(context.Request["UserID"]);
            int taskID = 0;
            if (context.Request["taskID"] != null)
            {
                taskID = Convert.ToInt32(context.Request["taskID"]);
            }
            PrizeTaskReocrd model = new PrizeTaskReocrd()
            {
                TaskID = taskID,
                Userid = UserID,
                Prizeid = prizeID,
                Createtime = DateTime.Now,
                EditTime = DateTime.Now,
                Remark = txtRemark,
                PrizeCount = txtPrizeCount,
                Stuas = Convert.ToInt32(EnumPrizeTaskRecordstate.未领取),
                IsManuallyAdd = txtIsManuallyAdd
            };
            int id = Controller.Add(model);
            context.Response.Write(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="context"></param>
        private void Update(HttpContext context)
        {
            int ID = Convert.ToInt32(context.Request["id"]);
            int prizeID = Convert.ToInt32(context.Request["prizeID"]);
            string txtRemark = context.Request["txtRemark"];
            int txtPrizeCount = Convert.ToInt32(context.Request["txtPrizeCount"]);
            int taskID = 0;
            if (context.Request["taskID"] != null)
            {
                taskID = Convert.ToInt32(context.Request["taskID"]);
            }
            PrizeTaskReocrd model = new PrizeTaskReocrd()
            {
                TaskID = taskID,
                Id = ID,
                Prizeid = prizeID,
                Remark = txtRemark,
                EditTime = DateTime.Now,
                PrizeCount = txtPrizeCount
            };
            context.Response.Write(Controller.Update(model));
        }
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["id"]);
            context.Response.Write(Controller.Delete(id));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}